package com.example.android.myapp.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.android.R;

public class FragmentSwitch extends FragmentActivity implements View.OnClickListener {
    
    FragmentManager mFragmentMgr;
    
    private Button btnLeft;
    
    private Button btnRight;
    
    private LeftFragment mLeftFragment;
    private RightFragment mRightFragment;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    mFragmentMgr = getSupportFragmentManager();
		setContentView(R.layout.layout_fragment_switch);
		
		btnLeft= (Button) findViewById(R.id.left);
		btnLeft.setOnClickListener(this);
		btnLeft.setClickable(false);
		
		btnRight = (Button) findViewById(R.id.right);
		btnRight.setOnClickListener(this);

		mLeftFragment = new LeftFragment();
		mRightFragment = new RightFragment();
		
		FragmentTransaction fragmentTransaction = mFragmentMgr.beginTransaction();
		fragmentTransaction.add(R.id.fragment_root, mLeftFragment);
		fragmentTransaction.add(R.id.fragment_root, mRightFragment);
		fragmentTransaction.hide(mRightFragment);
		fragmentTransaction.show(mLeftFragment);
		fragmentTransaction.commit();
	}
	
	public class LeftFragment extends BaseFragment {
	    @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	        super.onCreateView(inflater, container, savedInstanceState);
	        ImageView imgv = new ImageView(FragmentSwitch.this);
	        imgv.setImageResource(R.drawable.test_fly_1_3);
	        return imgv;
	    }
	}
	
	public class RightFragment extends BaseFragment {
	    @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	        super.onCreateView(inflater, container, savedInstanceState);
	        ImageView imgv = new ImageView(FragmentSwitch.this);
            imgv.setImageResource(R.drawable.test_fly_1_4);
            return imgv;
	    }
	}

    @Override
    public void onClick(View v) {
        
        switch (v.getId()) {
            case R.id.left: {
                btnLeft.setClickable(false);
                btnRight.setClickable(true);
                
                FragmentTransaction fragmentTransaction = mFragmentMgr.beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.anim_fragment_rightin, R.anim.anim_fragment_leftout);
                fragmentTransaction.hide(mRightFragment);
                fragmentTransaction.show(mLeftFragment);
                fragmentTransaction.commit();
                break;
            }
            case R.id.right: {
                btnLeft.setClickable(true);
                btnRight.setClickable(false);
                
                FragmentTransaction fragmentTransaction = mFragmentMgr.beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.anim_fragment_leftin, R.anim.anim_fragment_rightout);
                fragmentTransaction.hide(mLeftFragment);
                fragmentTransaction.show(mRightFragment);
                fragmentTransaction.commit();
                break;
            }
        }
    }
}